package com.ruoyi.wtww.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.jdsq.domain.JdysJdsqFj;
import com.ruoyi.lxgl.kylx.domain.LlglXmsb;
import com.ruoyi.wtww.domain.WtwwFj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.wtww.domain.Wtww;
import com.ruoyi.wtww.service.IWtwwService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 委托委外Controller
 *
 * @author huacai
 * @date 2024-08-12
 */
@RestController
@RequestMapping("/wtww/wtww")
public class WtwwController extends BaseController
{
    @Autowired
    private IWtwwService wtwwService;

    /**
     * 查询委托委外列表
     */
    @PreAuthorize("@ss.hasPermi('wtww:wtww:list')")
    @GetMapping("/list")
    public TableDataInfo list(Wtww wtww)
    {
        startPage();
        List<Wtww> list = wtwwService.selectWtwwList(wtww);
        return getDataTable(list);
    }

    /**
     * 导出委托委外列表
     */
    @PreAuthorize("@ss.hasPermi('wtww:wtww:export')")
    @Log(title = "委托委外", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Wtww wtww)
    {
        List<Wtww> list = wtwwService.selectWtwwList(wtww);
        ExcelUtil<Wtww> util = new ExcelUtil<Wtww>(Wtww.class);
        util.exportExcel(response, list, "委托委外数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Wtww> util = new ExcelUtil<Wtww>(Wtww.class);
        util.importTemplateExcel(response, "委托委外数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "委托委外", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('wtww:wtww:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Wtww> util = new ExcelUtil<Wtww>(Wtww.class);
        InputStream inputStream = file.getInputStream();
        List<Wtww> list = util.importExcel(inputStream );
        inputStream.close();
        int count = wtwwService.batchInsertWtww(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取委托委外详细信息
     */
    @PreAuthorize("@ss.hasPermi('wtww:wtww:query')")
    @GetMapping(value = "/{wtwwId}")
    public AjaxResult getInfo(@PathVariable("wtwwId") String wtwwId)
    {
        return success(wtwwService.selectWtwwByWtwwId(wtwwId));
    }

    /**
     * 新增委托委外
     */
    @PreAuthorize("@ss.hasPermi('wtww:wtww:add')")
    @Log(title = "委托委外", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Wtww wtww) {
        //插入用户ID
        wtww.setUserId(Math.toIntExact(getUserId()));
        return toAjax(wtwwService.insertWtww(wtww));
    }

    /**
     * 修改委托委外
     */
    @PreAuthorize("@ss.hasPermi('wtww:wtww:edit')")
    @Log(title = "委托委外", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Wtww wtww)
    {
        return toAjax(wtwwService.updateWtww(wtww));
    }

    /**
     * 删除委托委外
     */
    @PreAuthorize("@ss.hasPermi('wtww:wtww:remove')")
    @Log(title = "委托委外", businessType = BusinessType.DELETE)
	@DeleteMapping("/{wtwwIds}")
    public AjaxResult remove(@PathVariable String[] wtwwIds)
    {
        return toAjax(wtwwService.deleteWtwwByWtwwIds(wtwwIds));
    }

    /**
     * 获得委托委外主键ID (UUID)
     */
    @GetMapping("/getWtWwId")
    public AjaxResult getWtWwId(){
        String wtwwId = IdUtils.fastSimpleUUID();
        return success(wtwwId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertWtwwFjByWtwwId(@RequestBody WtwwFj wtwwFj){
        return toAjax(wtwwService.insertWtwwFjByWtwwId(wtwwFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public TableDataInfo selectWtwwFjList(WtwwFj wtwwFj){
        startPage();
        List<WtwwFj> list = wtwwService.selectWtwwFjList(wtwwFj);
        return getDataTable(list);
    }

    /**
     * 删除委托委外附件资料
     */
    @Log(title = "委托委外附件资料", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult remove(@PathVariable Integer[] fjIds) {
        return toAjax(wtwwService.deleteWtwwFjByFjIds(fjIds));
    }

    /**
     * 锁定委托委外
     */
    @PutMapping("/locking")
    @Log(title = "委托委外锁定", businessType = BusinessType.LOCKING)
    @PreAuthorize("@ss.hasPermi('wtww:wtww:locking')")
    public AjaxResult updateSdztLocking(@RequestBody Wtww wtww) {
        return toAjax(wtwwService.updateSdztLocking(wtww));
    }

    /**
     * 解锁委托委外
     */
    @PutMapping("/unlock")
    @Log(title = "委托委外解锁", businessType = BusinessType.UNLOCK)
    @PreAuthorize("@ss.hasPermi('wtww:wtww:unlock')")
    public AjaxResult updateSdztUnlock(@RequestBody Wtww wtww) {
        return toAjax(wtwwService.updateSdztUnlock(wtww));
    }

    /**
     * 获取委托委外与款项记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('jdsq:jdsq:query')")
    @GetMapping(value = "/kxjl/{wtwwId}")
    public AjaxResult selectWtwwKxJlByWtwwId(@PathVariable("wtwwId") String wtwwId) {
        return success(wtwwService.selectWtwwKxJlByWtwwId(wtwwId));
    }

}
